/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2025 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.module.modules.exploit.disabler.disablers

import net.ccbluex.liquidbounce.config.AutoConfig
import net.ccbluex.liquidbounce.config.types.nesting.ToggleableConfigurable
import net.ccbluex.liquidbounce.event.waitTicks
import net.ccbluex.liquidbounce.event.tickHandler
import net.ccbluex.liquidbounce.features.module.modules.exploit.disabler.ModuleDisabler
import net.ccbluex.liquidbounce.lang.translation
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.ccbluex.liquidbounce.utils.item.getEnchantment
import net.minecraft.enchantment.Enchantments
import net.minecraft.item.Items
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
import net.minecraft.network.packet.c2s.play.PlayerInteractItemC2SPacket
import net.minecraft.util.Hand
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Direction

/**
 * Exploits the Riptide except of Vulcan in order to disable any movement checks
 *
 * This seems to be patched in newer versions of Vulcan
 * This disabler was added in 02/07/23, which indicates it was probably made for Vulcan 2.7.5
 */
internal object DisablerVulcanRiptide : ToggleableConfigurable(ModuleDisabler, "VulcanRiptide", false) {

    override fun onEnabled() {
        if (AutoConfig.loadingNow) {
            return
        }

        if (!checkItem()) {
            chat(markAsError(translation("liquidbounce.module.disabler.messages.vulcanRiptideMessage")))
            enabled = false
        }
    }

    @Suppress("unused")
    val repeatable = tickHandler {
        if (checkItem()) {
            waitTicks(20)
            interaction.sendSequencedPacket(world) { sequence ->
                PlayerInteractItemC2SPacket(Hand.OFF_HAND, sequence, player.yaw, player.pitch)
            }
            network.sendPacket(
                PlayerActionC2SPacket(
                    PlayerActionC2SPacket.Action.RELEASE_USE_ITEM, BlockPos.ORIGIN, Direction.DOWN
                )
            )
        }
    }

    private fun checkItem(): Boolean {
        val offhand = player.offHandStack
        return (offhand.item == Items.TRIDENT && offhand.hasEnchantments() &&
            offhand.getEnchantment(Enchantments.RIPTIDE) != 0)
    }
}
